package com.gen.info.generate_cluster_information.controller;

import com.alibaba.excel.EasyExcel;
import com.gen.info.generate_cluster_information.Listener.ReadOceanBaseListener;
import com.gen.info.generate_cluster_information.config.MyExcel;
import com.gen.info.generate_cluster_information.mode.OceanBaseClusterInfo;
import com.gen.info.generate_cluster_information.mode.OceanBaseClusterInfoDO;
import com.gen.info.generate_cluster_information.mode.RSPoolVO;
import com.gen.info.generate_cluster_information.mode.RSUnitVO;
import com.gen.info.generate_cluster_information.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.HashMap;

@Component
public class MainTask{
    @Autowired
    private  CreateTenant createTenant;
    @Autowired
    private  CreateResourcePool createResourcePool;
    @Autowired
    private  CreateUnit createUnit;
    @Autowired
    private  CreateDatabaseUser createDatabaseUser;
    @Autowired
    private  GrantUserAuthority grantUserAuthority;
    @Autowired
    private MyExcel myExcel;
    public  void CreateClusterRS(){
        /*
        * 启动easyExcel
        * */
        // 实例化读取参
        // 直接读取数据，不指定表头行号，默认从第一行开始读取数据
        EasyExcel.read(myExcel.getPath(), OceanBaseClusterInfo.class, new ReadOceanBaseListener()).sheet().doRead();
        //获取集群信息资源
        final Collection<OceanBaseClusterInfoDO> clusterInfoDOS = ReadOceanBaseListener.clusterInfos.values();
        /*创建资源单元
        * */
        final HashMap<String, RSUnitVO> unitRS = createUnit.getUnitRS(clusterInfoDOS);
        createUnit.createUnitRS(unitRS);
        /*创建资源池
        * */
        final HashMap<String, RSPoolVO> rsPool = createResourcePool.getRSPool(clusterInfoDOS, unitRS);
        createResourcePool.createResourcePool(rsPool);
        /*
        * 创建租户
        * */
        createTenant.CreateTenantRS(createTenant.getTenantRS(clusterInfoDOS,rsPool));
        /*
        * 创建数据库和用户
        * */
        createDatabaseUser.createDBRS(clusterInfoDOS);
        /*给用户授予对应数据库的权限*/
        grantUserAuthority.grantAuthority(clusterInfoDOS);
    }


}
